package com.kangnam.mongo;

import java.util.Scanner;

import com.kangnam.mongo.MongoManager;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;

public class DeleteProduct {
	private static MongoManager mongoManager = null;
	private static String DB_NAME;
	private static String P_INFO = "product";
	public DeleteProduct(String dbName, String host)
	{
		mongoManager = MongoManager.getInstance();
		mongoManager.createPool(host);
		DB_NAME = dbName;	
	}
	
	public static void searchID(String collection, String field, String value){
		
		Mongo mongo = null;
		DBObject obj = null;
		
		try{
			mongo = mongoManager.getConnection();
			DB db = mongo.getDB(DB_NAME);
			
			BasicDBObject query = new BasicDBObject();
			query.append(field, value);
			
			DBCollection coll = db.getCollection(collection);		
			DBCursor cursor = coll.find(query);
			while (cursor.hasNext()){
				DBObject dbObject = (DBObject) cursor.next();
				System.out.println(dbObject);
			}

		} finally {
			mongoManager.close(mongo);
		}
		
		
	}
	
	public static void Delete_P(String collection, String field, String value){
		Mongo mongo = null;
		DBObject obj = null;
		
		try{
			mongo = mongoManager.getConnection();
			DB db = mongo.getDB(DB_NAME);
			
			BasicDBObject query = new BasicDBObject();
			query.append(field, value);
			
			DBCollection coll = db.getCollection(collection);		
			obj = coll.findOne(query);
			
			
			

		} finally {
			mongoManager.close(mongo);
		}
		
		
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		DeleteProduct InputFT = new DeleteProduct("db", "127.0.0.1");
		Scanner Inputname = new Scanner(System.in);
		String name = Inputname.nextLine();
		
		InputFT.searchID(P_INFO, "p_Name", name);
		
		Scanner InputID = new Scanner(System.in);
		String pro_ID = Inputname.nextLine();
		
		InputFT.Delete_P(P_INFO,"p_ID", pro_ID);
		InputFT.searchID(P_INFO, "p_Name", name);
	}

}
